package cn.itcast.p2p20.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;

/**
 * Created by Administrator on 2016/7/29.
 */
public class FlyLayout extends FrameLayout implements Animation.AnimationListener {

    private static final double MAX_VELOCITY_TOUCH_SLOP = 80;
    private GestureDetector mGestureDetector;

    public FlyLayout(Context context) {
        this(context, null);
    }

    public FlyLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private boolean animationRunning;

    public FlyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                if (!animationRunning && Math.hypot(velocityX, velocityY) > MAX_VELOCITY_TOUCH_SLOP) {

                    playAnimation();
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    private void playAnimation() {

        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.ABSOLUTE, 0.5f);
        animationSet.addAnimation(scaleAnimation);
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        //透明度动画延迟1秒执行
        alphaAnimation.setStartOffset(1000);
        animationSet.addAnimation(alphaAnimation);


        animationSet.setDuration(2000);
        animationSet.setFillAfter(true);
        animationSet.setAnimationListener(this);
        //上面的的界面
        getChildAt(getChildCount() - 1).startAnimation(animationSet);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        return super.onInterceptTouchEvent(ev);
    }

    /* @Override
    public boolean onTouchEvent(MotionEvent event) {

        mGestureDetector.onTouchEvent(event);
        return true;
    }*/

    @Override
    public void onAnimationStart(Animation animation) {

        animationRunning = true;
    }

    @Override
    public void onAnimationEnd(Animation animation) {

        //View的动画结束了只是动画停止执行了，但是动画依然保留在View 里面，开启新的动画有可能不会再执行，
        //需要在开启新的动画之前把原来的动画清除

        //动画执行完毕交换两个子控件的位置
        View topView = getChildAt(getChildCount() - 1);

        //topView.clearAnimation();会触发动画监听器的执行
        //在topView.clearAnimation();之前将动画监听移除
        animation.setAnimationListener(null);
        //清除原来的动画
        topView.clearAnimation();

        removeView(topView);
        addView(topView, 0);

        animationRunning = false;
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}
